package com.jiulong.service.Impl;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.jiulong.dao.TGiftMapper;
import com.jiulong.dao.TOrderMapper;
import com.jiulong.dao.TOrderdetailMapper;
import com.jiulong.dao.TOrderlogMapper;
import com.jiulong.dao.TOrdershipMapper;
import com.jiulong.dao.TUserMapper;
import com.jiulong.model.TAccount;
import com.jiulong.model.TAccountExample;
import com.jiulong.model.TGift;
import com.jiulong.model.TGiftExample;
import com.jiulong.model.TIndexImg;
import com.jiulong.model.TIndexImgExample;
import com.jiulong.model.TOrder;
import com.jiulong.model.TOrderExample;
import com.jiulong.model.TOrderdetail;
import com.jiulong.model.TOrderlog;
import com.jiulong.model.TOrdership;
import com.jiulong.model.TUser;
import com.jiulong.model.TUserExample;
import com.jiulong.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    TOrderMapper tOrderMapper;
    @Autowired
    com.jiulong.dao.TIndexImgMapper TIndexImgMapper;
    @Autowired
    TOrdershipMapper TOrdershipMapper;
    @Autowired
    TOrderlogMapper TOrderlogMapper;
    @Autowired
    TGiftMapper tGiftMapper;
    @Autowired
    TUserMapper TUserMapper;
    @Autowired
    TOrderdetailMapper TOrderdetailMapper;

    @Override
    public TOrder insertOrder(TOrder order) {


        tOrderMapper.insert(order);
        return null;
    }

    @Override
    public List findOrderDetailByOpenid(String account) {


        TOrderExample example = new TOrderExample();
        example.createCriteria().andAccountEqualTo(account).andPaystatusEqualTo("n");
        List<TOrder> list = tOrderMapper.selectByExample(example);

        //System.out.println("我的订单回显到前端页面的价格"+list.get(0).getAmount());
    /*	System.out.println(list.get(0).getExpressno());
		System.out.println(list.get(1).getAccount());
		System.out.println(list.get(2).getAccount());*/
        return list;
    }

    @Override
    public List findOrderDetailByOpenidAleadyPay(String account) {


        TOrderExample example = new TOrderExample();
        example.createCriteria().andAccountEqualTo(account).andPaystatusEqualTo("y");
        List<TOrder> list = tOrderMapper.selectByExample(example);

        //System.out.println("我的订单回显到前端页面的价格"+list.get(0).getAmount());
	/*	System.out.println(list.get(0).getExpressno());
		System.out.println(list.get(1).getAccount());
		System.out.println(list.get(2).getAccount());*/
        return list;
    }

    @Override
    public List findGiftbyId(String nickname) {

        TGiftExample example = new TGiftExample();
        example.createCriteria().andStatusEqualTo(nickname);
        List<TGift> gift = tGiftMapper.selectByExample(example);

        return gift;
    }

    @Override
    public TOrderdetail insertOrderDetail(TOrderdetail detail) {
        System.out.println("正在插入详细信息");
        TOrderdetailMapper.insert(detail);
        return null;
    }

    @Override
    public TOrdership insertOrderShip(TOrdership ship) {
        System.out.println("正在插入ship详细信息");
        TOrdershipMapper.insert(ship);
        return null;

    }

    @Override
    public TOrderlog insertOrderLog(TOrderlog log) {
        System.out.println("正在插入log详细信息");
        TOrderlogMapper.insert(log);
        return null;
    }

    //分店审核的查询
    @Override
    public List findOrderDetailWithNoCheck(String username, String dateBegin, String dateEnd) {
        TOrderExample example = new TOrderExample();

        example.createCriteria().andUserEqualTo(username).andPaystatusEqualTo("y").andCreatedateBetween(dateBegin, dateEnd);
        //example.setOffset(5);
        //example.setLimit(5);
        List<TOrder> list = tOrderMapper.selectByExample(example);
        return list;
    }

    //查询总盈利
    @Override
    public String findOrderDetailWithNoCheck1(String username, String dateBegin, String dateEnd, long counts) {
        TOrderExample example = new TOrderExample();
        example.createCriteria().andUserEqualTo(username).andPaystatusEqualTo("y").andCreatedateBetween(dateBegin, dateEnd);
        List<TOrder> list = tOrderMapper.selectByExample(example);
        double totalAmount2 = 0;
        String totalAmount22 = null;
        if (list.toString() == "[]") {
            return "error";
        }
        try {
            for (int i = 0; i < counts; i++) {
                String totalAmount = list.get(i).getFee().toString();
                double totalAmount1 = Double.parseDouble(totalAmount);
                totalAmount2 = totalAmount2 + totalAmount1;
            }
            totalAmount22 = String.valueOf(totalAmount2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return totalAmount22;
    }


    @Override
    public List findOrderDetailWithBeenCheck(String username, String dateBegin,
                                             String dateEnd) {
        TOrderExample example = new TOrderExample();
        example.createCriteria().andCarryEqualTo(1).andUserEqualTo(username).andPaystatusEqualTo("y").andCreatedateBetween(dateBegin, dateEnd);

        List<TOrder> list = tOrderMapper.selectByExample(example);
		/*for(int i=0;i<list.size();i++){
			list.get(i)
		}*/
        return list;
    }

    @Override
    public List findOrderDoing() {
        TIndexImgExample example = new TIndexImgExample();
        example.setOrderByClause("desc1");
        List<TIndexImg> list = TIndexImgMapper.selectByExample(example);
        return list;
    }

    @Override
    public List findOrderDetailWithTi(String picture) {
        TOrderExample example = new TOrderExample();


        example.createCriteria().andOtherrequirementEqualTo(picture);
        List<TOrder> list = tOrderMapper.selectByExample(example);
        return list;
    }

    @Override
    public String findOrderDetailWithTi1(String picture) {

        TOrderExample example = new TOrderExample();
        example.createCriteria().andPaystatusEqualTo("y").andOtherrequirementEqualTo(picture);
        List<TOrder> list = tOrderMapper.selectByExample(example);
        long counts = tOrderMapper.countByExample(example);
        if (list.toString() == "[]") {
            return "error";
        }
        double totalAmount2 = 0;
        String totalAmount22 = null;
        try {
            for (int i = 0; i < counts; i++) {
                String totalAmount = list.get(i).getFee().toString();
                double totalAmount1 = Double.parseDouble(totalAmount);
                totalAmount2 = totalAmount2 + totalAmount1;
            }
            totalAmount22 = String.valueOf(totalAmount2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return totalAmount22;
    }

    //通过分店提交的未审核进行查询
    @Override
    public List findOrderorderCheckCommit(String flag) {
        TIndexImgExample example = new TIndexImgExample();
        example.createCriteria().andLinkEqualTo("账单待审核").andTitleEqualTo(flag);
        List<TIndexImg> list = TIndexImgMapper.selectByExample(example);
        return list;
    }
}
